<?php

class RoleUserModel extends BaseModel
{
    public $_validate = array(
        array('role_id', 'require', '角色必须'),
        array('user_id', 'require', '用户必须'),
    );

    /**
     * 获取用户角色信息
     * @param  $uid 用户标识
     * @return Array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getUserRloe($uid)
    {
        $map = array();
        $map['user_id'] = $uid;
        $roles = $this->where($map)->select();
        $RoleModel = M('Role');
        foreach ($roles as $key => $role) {
            $r = $RoleModel->find($role['role_id']);
            $roles[$key]['role_name'] = $r['name'];
            $roles[$key]['role_remark'] = $r['remark'];
        }
        return $roles;
    }

    /**
     * 获取用户角色信息
     * @param  $uid 用户标识
     * @return Array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function setUserRloe($uid, $roles)
    {
        //删除以前的权限记录
        $rs = $this->query('DELETE FROM `m_role_user` WHERE ( user_id = '.$uid.' )');
        //添加现有纪录
        foreach ($roles as $role) {
            $data['user_id'] = $uid;
            $data['role_id'] = $role;
            $this->add($data);
        }
    }

    /**
     * 获取用户角色信息
     * @param  $uid 用户标识
     * @return Array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getUserOtherRloe($uid)
    {
        $data = $this->query('SELECT * FROM m_role WHERE id NOT IN ( SELECT role_id FROM m_role_user WHERE user_id = ' . $uid . ')');
        return $data;
    }
}

?>